<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/13
 * Time: 15:44
 */

namespace app\modules\controllers;

use app\modules\models\Admin;
use Yii;
use yii\web\Controller;

class PublicController extends Controller
{
//    ?r=admin/public/login
    public function actionLogin()
    {
        $model=new Admin();
        if(Yii::$app->request->isPost){
            $post=Yii::$app->request->post();
            if($model->login($post)){
                $this->redirect(['default/index']);
//                redirect不能终止程序 必须使用return 或者 Yii::$app->end(); 如果是在程序的末尾，就没事，因为程序后面
                Yii::$app->end();
            }
        }
        return $this->renderPartial('login',['model'=>$model]);
    }

    public function actionLogout()
    {
        Yii::$app->session->removeAll();
        if(!isset(Yii::$app->session['admin']['isLogin'])){
                $this->redirect(['Public/login']);
                Yii::$app->end();
        }
        $this->goBack();
    }

    public function actionSeekpassword()
    {
        $this->layout = false;
        $model = new Admin;
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->seekPass($post)) {
                Yii::$app->session->setFlash('info', '电子邮件已经发送成功，请查收');
            }
        }
        return $this->render("seekpassword", ['model' => $model]);
    }

}